package com.anjiplus.template.gaea.business.modules.report.service.impl;

import com.anji.plus.gaea.constant.BaseOperationEnum;
import com.anji.plus.gaea.curd.mapper.GaeaBaseMapper;
import com.anji.plus.gaea.exception.BusinessException;
import com.anji.plus.gaea.holder.UserContentHolder;
import com.anjiplus.template.gaea.business.modules.accessrole.dao.AccessRoleReportMapper;
import com.anjiplus.template.gaea.business.modules.accessrole.dao.entity.AccessRoleReport;
import com.anjiplus.template.gaea.business.modules.accessuser.dao.AccessUserRoleMapper;
import com.anjiplus.template.gaea.business.modules.accessuser.dao.entity.AccessUserRole;
import com.anjiplus.template.gaea.business.modules.report.controller.dto.ReportDto;
import com.anjiplus.template.gaea.business.modules.report.dao.ReportMapper;
import com.anjiplus.template.gaea.business.modules.report.dao.entity.Report;
import com.anjiplus.template.gaea.business.modules.report.service.ReportService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenkening
 * @date 2021/3/26 10:35
 */
@Service
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportMapper reportMapper;

    @Autowired
    private AccessUserRoleMapper accessUserRoleMapper;

    @Autowired
    private AccessRoleReportMapper accessRoleReportMapper;

    @Override
    public GaeaBaseMapper<Report> getMapper() {
        return reportMapper;
    }

    @Override
    public List<String> getReportListByRoles() {
        List<String> roleList = accessUserRoleMapper.selectList(Wrappers.<AccessUserRole>lambdaQuery().eq(AccessUserRole::getLoginName, UserContentHolder.getContext().getUsername())).stream().map(accessUserRole -> accessUserRole.getRoleCode()).distinct().collect(Collectors.toList());
        List<AccessRoleReport> accessRoleReportList = accessRoleReportMapper.selectList(Wrappers.<AccessRoleReport>lambdaQuery().in(AccessRoleReport::getRoleCode, roleList));
        return accessRoleReportList.stream()
                .map(accessRoleReport -> accessRoleReport.getReportCode()).distinct().collect(Collectors.toList());
    }

    @Override
    public void delReport(ReportDto reportDto) {
        deleteById(reportDto.getId());
        //删除gaea_report_excel、gaea_report_dashboard、gaea_report_dashboard_widget
        //...
    }

    /**
     * 下载次数+1
     *
     * @param reportCode
     */
    @Override
    public void downloadStatistics(String reportCode) {
        Report report = selectOne("report_code", reportCode);
        if (null != report) {
            Long downloadCount = report.getDownloadCount();
            if (null == downloadCount) {
                downloadCount = 0L;
            } else {
                downloadCount++;
            }
            report.setDownloadCount(downloadCount);
            update(report);
        }

    }

    @Override
    public void processBeforeOperation(Report entity, BaseOperationEnum operationEnum) throws BusinessException {

    }
}
